home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0697.zip / NELSON.ZIP / JUN97.CPP
C/C++ Source or Header  |  1997-02-07  |  2KB  |  72 lines

  1. //
  2. // JUN97.CPP
  3. //
  4. // This program demonstrates a code generation problem
  5. // in Microsoft Visual C++ 4.2.  If you compile this
  6. // with the following command line:
  7. //
  8. //  cl /Fc /Ox /GX jun97.cpp
  9. //
  10. // you will get an executable that crashes with a GPF.
  11. // The problem is that the file pointer, fgif, in
  12. // Gif::Load() is corrupted immediately after jumping
  13. // over the first exception call.  Consequently, the
  14. // next call to fgetc() is made with a bogus file pointer,
  15. // resulting in a crash.
  16. //
  17. // The code that first illuminated the problem was part
  18. // of a Gif class.  I've stripped as much code as possible
  19. // to simplify, so don't try to make too much sense of
  20. // what's left!
  21. //
  22.  
  23. #include <stdio.h>
  24.  
  25. struct Pixel {
  26.     Pixel() : m_value( 0 ) {};
  27.     unsigned char m_value;
  28. };
  29.  
  30. struct Gif {
  31.     void Load();
  32.     Pixel *m_pPixels;
  33.     void Init() {
  34.         m_pPixels  = new Pixel[ 20 ];
  35.     }
  36. };
  37.  
  38. void Gif::Load()
  39. {
  40.     FILE *fgif = fopen( "jun97.cpp", "rb" );
  41.  
  42.     if ( fgif ) {
  43.         Init();
  44.         for ( int i = 0 ; i < 10 ; i++ )
  45.             m_pPixels[ i ].m_value = fgetc( fgif );
  46.         if ( feof( fgif ) || ferror( fgif ) )
  47.             throw "Error reading global color table";
  48.     }
  49.     //
  50.     // At this point, the code generator has loaded esi
  51.     // with a bogus value, but it gets passed to fgetc()
  52.     // as if it were a valid pointer to fgif
  53.     //
  54.     int type;
  55.     while( ( type = fgetc( fgif ) ) > 0 ) {
  56.         if ( type == 0x3B )
  57.             break;
  58.         throw "Unknown or invalid construct";
  59.     }
  60.     if ( type == 0 )
  61.         throw "No images in GIF file!";
  62.     fclose( fgif );
  63. }
  64.  
  65. int main()
  66. {
  67.     Gif gif;
  68.     gif.Load();
  69.     return 0;
  70. }
  71.  
  72.